package com.ethereal.petmanagement.dao.impl;

import com.ethereal.petmanagement.dao.IUserDao;
import com.ethereal.petmanagement.dto.ResponseDTO;
import com.ethereal.petmanagement.dto.UserDTO;
import com.ethereal.petmanagement.util.CodeMsg;
import com.ethereal.petmanagement.util.DatabaseUtil;

import java.math.BigInteger;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class UserDaoImpl implements IUserDao {
    //按照手机号查找用户（登录功能）
    @Override
    public ResponseDTO<UserDTO> findUserByPhone(UserDTO userDTO) {
        String sql = "select * from user where phone = ?";
        try(
                Connection conn = DatabaseUtil.getConnection(); //连接数据库
                PreparedStatement pstmt = conn.prepareStatement(sql); //配置sql
                ){
            //添加参数
            pstmt.setString(1, userDTO.getPhone());
            ResultSet rs = pstmt.executeQuery(); //查找结果
            if (rs.next()){
                userDTO.setPhone(rs.getString("phone"));
                userDTO.setUsername(rs.getString("username"));
                userDTO.setId((BigInteger) rs.getObject("id"));
                userDTO.setPassword(rs.getString("password"));
                userDTO.setSalt(rs.getString("salt"));
            }
            return ResponseDTO.success(userDTO);
        } catch (Exception e) {
            return ResponseDTO.errorByMsg(CodeMsg.DATABASE_ERROR);
        }
    }

    //添加用户（注册功能）
    @Override
    public Integer insertUser(UserDTO userDTO) {
        String sql = "insert into user (id, username, password, phone, salt, create_time, address) values (?, ?, ?, ?, ?, ?, ?)";
        int rows;
        try (
                Connection conn = DatabaseUtil.getConnection(); //连接数据库
                PreparedStatement pstmt = conn.prepareStatement(sql); //配置sql语句
        ) {
            //添加参数
            pstmt.setObject(1, userDTO.getId());
            pstmt.setString(2, userDTO.getUsername());
            pstmt.setString(3, userDTO.getPassword());
            pstmt.setString(4, userDTO.getPhone());
            pstmt.setString(5, userDTO.getSalt());
            pstmt.setTimestamp(6, userDTO.getCreateTime());
            pstmt.setString(7, userDTO.getAddress());
            //操作数据库，将返回值给rows
            rows = pstmt.executeUpdate();
            conn.commit(); //永久更新
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return rows;
    }

    //修改用户信息
    @Override
    public Integer updateUser(UserDTO userDTO) {
        String sql = "update user set username = ?, phone = ?, address = ?, avatar_url = ?, update_time = ? where id = ?";
        int rows;
        try(
                Connection conn = DatabaseUtil.getConnection();
                PreparedStatement pstmt = conn.prepareStatement(sql)
                ) {
            //添加参数
            pstmt.setString(1, userDTO.getUsername());
            pstmt.setString(2, userDTO.getPhone());
            pstmt.setString(3, userDTO.getAddress());
            pstmt.setString(4, userDTO.getAvatarUrl());
            pstmt.setTimestamp(5, userDTO.getUpdateTime());
            pstmt.setObject(6, userDTO.getId());
            //执行sql
            rows = pstmt.executeUpdate();
            conn.commit(); //永久更新
        }catch (Exception e){
            throw new RuntimeException(e);
        }
        return rows;
    }

    //删除用户
    @Override
    public Integer deleteUser(UserDTO userDTO) {
        String sql = "delete from user where id = ? ";
        int rows;
        try(
                Connection conn = DatabaseUtil.getConnection();
                PreparedStatement pstmt = conn.prepareStatement(sql)
        ) {
            //添加参数
            pstmt.setObject(1, userDTO.getId());
            //执行sql
            rows = pstmt.executeUpdate();
            conn.commit(); //永久更新
        }catch (Exception e){
            throw new RuntimeException(e);
        }
        return rows;
    }
}
